﻿using CampusTrade.Pojo;
using Furion.DependencyInjection;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CampusTrade.Dao
{
    public class ConsultDao : ITransient
    {
        private readonly ISqlSugarRepository repository;
        private readonly SqlSugarClient db;
        public ConsultDao(ISqlSugarRepository sqlSugarRepository)
        {
            repository = sqlSugarRepository;
            db = repository.Context;
        }

        public long addQuestion(Question question)
        {
            try
            {
                return db.Insertable(question).ExecuteReturnBigIdentity();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return -1;
            }
        }

        public long addAnswer(Answer answer)
        {
            try
            {
                return db.Insertable(answer).ExecuteReturnBigIdentity();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return -1;
            }
        }

        public int deleteQuestion(long id)
        {
            try
            {
                return db.Deleteable<Question>().In(id).ExecuteCommand();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return -1;
            }
        }

        public int deleteAnswer(long id)
        {
            try
            {
                return db.Deleteable<Answer>().In(id).ExecuteCommand();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return -1;
            }
        }

        public int deleteById(long id)
        {
            return -1;
        }

        public List<Answer> getAnswer(long id)
        {
            return db.Queryable<Answer>().Where(it => it.QuestionId == id).ToList();
        }

        public List<Question> getQuestion(long id)
        {
            return db.Queryable<Question>().Where(it => it.GoodId == id).ToList();
        }

    }
}
